import utils from '../utils'
import config from '../js/config'
const api_endpoint = config.api_endpoint
// fetch get wrapper
const  _fetch = (url, data) => {
    return fetch(url + utils.param(data), {
        method: 'get',
        // mode: 'cors', // 允许跨域
        credentials: 'same-origin', // include
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        }
    }).then(function(response) { 
        return response.json()
    })
}

const _post = (url, data) => {
    return fetch(url, {
        method: 'post',
        // mode: 'cors',  // 允许跨域
        credentials: 'same-origin', // include
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        body: JSON.stringify(data)
    }).then(function(response) { 
        return response.json()
    })
}


// 用户相关API
class User {
    // 用户登录
    login(return_url){
        // console.log(location.href);
        // window.location.href = api_endpoint + '/auth/wx_authorize?return_url=' + encodeURIComponent(location.href)
    }

    /**
     * 获取微信签名
     * GET /user/wxsign
     * @memberof user
     */
    async wxsign(url){
        return  _fetch(api_endpoint + '/weixin/sign_package?url=' + url)
    }

    /**
     * 获取用户信息
     * GET /user/info
     * @memberof user
     */
    async info(order_id = 0){
        return  _fetch(api_endpoint + '/user/info?')
    }

    /**
     * 获取用户历史评论 [我的评论]
     * GET /comment/my
     * @memberof user
     */
    async comments(page = 1, page_size = 50){
        return  _fetch(
            api_endpoint + '/comment/my?',
            {
                page,
                page_size
            }
        )
    }

    /**
     * 修改用户密码
     * GET /user/password
     * @memberof user
     */
    async password(old_password, new_password, re_new_password){
        return  _post(
            api_endpoint + '/user/password?',
            {
                old_password,
                new_password,
                re_new_password
            }
        )
    }

    /**
     * 修改用户基本信息
     * GET /user/update
     * @param {object} info  用户信息
     * @param {string} info.name * 用户姓名
     * @param {string} info.avatar * 用户头像
     * @param {string} info.mobile * 手机号码
     * @param {string} info.email * 用户邮箱
     * @param {string} info.gender * 用户性别
     * @param {string} info.company * 公司名称
     * @param {string} info.job * 用户职位
     * 
     * @memberof user
     */
    async updateInfo(info){
        return  _post(api_endpoint + '/user/update?', info)
    }

    /**
     * @desc 添加收藏会议
     * @param 
     */
    async addCollect(meeting_id){
        return  _post(
            api_endpoint + '/user/collection/add', 
            {
                meeting_id
            }
        )
    }

    /**
     * @desc 取消收藏会议
     * @param 
     */
    async removeCollect(meeting_id){
        return  _post(
            api_endpoint + '/user/collection/remove', 
            {
                meeting_id: [meeting_id]
            }
        )
    }

    /**
     * @desc 判断是否已经收藏会议
     * @param 
     */
    async isCollect(meeting_id){
        return  _fetch(
            api_endpoint + '/user/collection/isadd?', 
            {
                meeting_id
            }
        )
    }

    /**
     * @desc 用户收藏会议列表
     * @param 
     */
    async collectList(){
        return  _fetch(api_endpoint + '/user/collection/list')
    }
}

// 评论功能API
class Comment {
    // 添加评论
    create(id, content ){
        return  _post(api_endpoint + '/comment/guest/create?', {
            id,
            content
        })
    }

    // 评论列表
    list(id, page = 1, page_size = 50){
        return  _fetch(api_endpoint + '/comment/guest/list?', {
            id,
            page,
            page_size
        })
    }
}

class File {
    // 文件上传
    upload(param){
        return  _post(api_endpoint + '/file/upload?', {
            ...param
        })
    }
}

class Article {
    // 文章列表
    list(category_id=0, page = 1, page_size = 20){
        return _fetch(api_endpoint + '/message/list?', {
            category_id,
            page,
            page_size
        })
    }
    // 文章模糊搜索
    search(q, category_id=0, page = 1, page_size = 20){
        return _fetch(api_endpoint + '/message/list?', {
            q,
            category_id,
            page,
            page_size
        })
    }
    //文章详情
    detail(id=0){
        return _fetch(api_endpoint + '/message/view?', {
            id
        })
    }
}

class Hospital {
    // 文章列表
    list(page = 1, page_size = 20){
        return  _fetch(api_endpoint + '/hospital/list?', {
            page,
            page_size
        })
    }
}

// 汇总API
const API  = {
    user: new User(),
    comment: new Comment(),
    file: new File(),
    hospital: new Hospital(),
    article: new Article()
}

export default API